﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.WeChat.Dtos.MPTag;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 公众号用户
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-09-18
    /// </summary>
    [Route("api/WeChat/MPTag")]
    public class MPTagController : BaseApiController
    {
        private MPTagBll mPTagBll = new MPTagBll();
        private MPUserBll mPUserBll = new MPUserBll();

        #region Get
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetTreeViewTag([FromQuery] KeywordInput input)
        {
            var tagsList = await mPTagBll.GetTageList();
            if (!input.keyword.IsNullOrEmpty())
            {
                tagsList = tagsList.FindAll(t => t.name.Contains(input.keyword));
            }
            var treeList = tagsList.Select(x => new MPTagTreeOutput()
            {
                fullName = x.name,
                id = x.id.ToString(),
                parentId = "0",
                icon = "fa fa-user"
            }).ToList();
            treeList.Add(new MPTagTreeOutput { id = "user", fullName = "全部用户", parentId = "0", icon = "fa fa-user" });
            return Success(new { list = treeList.ToTree() });
        }

        /// <summary>
        /// 标签
        /// </summary>
        /// <returns></returns>
        [HttpGet("Tree")]
        public async Task<ResponseResult> GetTageList()
        {
            var tagsList = await mPTagBll.GetTageList();
            var treeList = tagsList.Select(x => new MPTagListOutput()
            {
                fullName = x.name,
                id = x.id.ToString(),
                parentId = "0",
                icon = "fa fa-user"
            }).ToList();
            treeList.Add(new MPTagListOutput { id = "user", fullName = "全部用户", parentId = "0", icon = "fa fa-user" });
            return Success(new { list = treeList.ToTree() });
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("Tags")]
        public async Task<ResponseResult> GetTagList()
        {
            var tagsList = await mPTagBll.GetTageList();
            var treeList = tagsList.MapToList<MPTagTagsOutput>();
            return Success(treeList.ToTree());
        }
        #endregion

        #region Post
        /// <summary>
        /// 创建标签
        /// </summary>
        /// <param name="input">标签模型</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> CreateTag([FromBody] MPTagCrInput input)
        {
            await mPTagBll.CreateTag(input.fullName);
            return Success("新建成功");
        }

        /// <summary>
        /// 编辑标签
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> UpdateTag(int id, [FromBody] MPTagUpInput input)
        {
            var tagModel = (await mPTagBll.GetTageList()).Find(x => x.id == id);
            tagModel.name = input.fullName;
            await mPTagBll.UpdateTag(tagModel);
            return Success("编辑成功");
        }

        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id">标签id</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> DeleteTag(int id)
        {
            await mPTagBll.DeleteTag(id);
            return Success("删除成功");
        }

        /// <summary>
        /// 设置标签
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPost("Actions/SetTag")]
        [AllowAnonymous]
        public async Task<ResponseResult> SetTagged([FromBody] MPTagActionsSetTagInput input)
        {
            int id = -1;
            var tags = input.tagId.ToString().Split(',');
            string tagId = tags[0];
            MPUserModel userModel = input.MapTo<MPUserModel>();
            var userInfo = await mPUserBll.UserInfo(userModel.openid);
            foreach (var item in userInfo.tagid_list)
            {
                if (tagId.ToInt() == item)
                {
                    id = item;
                    break;
                }
            }
            if (tagId.ToInt() == id)
                //批量为用户取消标签
                await mPTagBll.BatchUnTagged(userModel, tagId);
            else
                //批量为用户打标签
                await mPTagBll.BatchTagged(userModel, tagId);
            return Success("操作成功");
        }
        #endregion
    }
}